<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/7/1
 * Time: 8:37 AM
 */
namespace app\backend\modules\content\services;
use app\common\models\Category;

class CategoryService
{
    /**
     * 获取分类列表
     * @param $params
     * @return mixed
     */
    public function getList($params){
        $query = Category::site()->select();
        if($params['name']){
            $query->where('name','like','%'.$params['name'].'%');
        }
        if($params['pid']){
            $query->where('pid',$params['pid']);
        }
        if(is_numeric($params['type']) === true || !empty($params['type'])){
            $query->where('type',$params['type']);
        }
        $list = $query
            ->with(['parent' => function($query) {
                return $query->select('id','pid','name','type');
            }])
            ->orderBy('id', 'desc')
            ->paginate(Category::PAGE_SIZE);
        $list->map(function ($v){
            $v->type_desc = Category::$typeMap[$v->type];
            $v->pname = '一级分类';
            if($v['parent'] && $v['parent']['name']){
                $v->pname = $v['parent']['name'];
            }
        });
        return $list;
    }
}
